package flc.ast.fragment;

import static flc.ast.utils.ModuleCategory.*;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupPosition;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.activity.CampusActActivity;
import flc.ast.activity.CertificateActivity;
import flc.ast.activity.InternshipExpActivity;
import flc.ast.activity.JobIntentionActivity;
import flc.ast.R;
import flc.ast.activity.AllResumeActivity;
import flc.ast.activity.BasicInfoActivity;
import flc.ast.activity.EducationExpActivity;
import flc.ast.activity.OtherInfoActivity;
import flc.ast.activity.PreviewActivity;
import flc.ast.activity.ProfessionalSkillActivity;
import flc.ast.activity.ProjectExpActivity;
import flc.ast.activity.WorkExpActivity;
import flc.ast.adapter.ExpAdapter;
import flc.ast.adapter.SkillAdapter;
import flc.ast.bean.ExpBean;
import flc.ast.bean.ResumeBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.dialog.ModuleManageDrawer;
import flc.ast.utils.CalculatorUtil;
import flc.ast.utils.Content;
import flc.ast.utils.ImageToPdfUtils;
import flc.ast.utils.ModuleCategory;
import flc.ast.utils.OkHttpPostUtil;
import flc.ast.utils.ResumeDataUtils;
import flc.ast.utils.ViewToBitmapUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {
    private List<ResumeBean> listShow = new ArrayList<>();
    private int selPos;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.tvManage.setOnClickListener(this);
        mDataBinding.ivAllResume.setOnClickListener(this);
        mDataBinding.ivGenerate.setOnClickListener(this);
        mDataBinding.tvCreateResume.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvManage://管理
                if (listShow.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_create_resume));
                    return;
                }
                if (selPos >= listShow.size()) {
                    selPos = listShow.size() - 1;
                }
                ModuleManageDrawer moduleManageDrawer = new ModuleManageDrawer(mContext);
                moduleManageDrawer.position = selPos;
                moduleManageDrawer.setListener(new ModuleManageDrawer.Ilistener() {
                    @Override
                    public void refresh() {
                        getData();
                    }

                    @Override
                    public void clickDelete() {
                        if (selPos != 0) selPos--;
                        getData();
                    }
                });
                new XPopup.Builder(getContext())
                        .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                        .popupPosition(PopupPosition.Left)//左边
                        .hasStatusBarShadow(true) //启用状态栏阴影
                        .asCustom(moduleManageDrawer)
                        .show();
                selPos++;
                if (selPos == listShow.size()) {
                    selPos = 0;
                }
                break;
            case R.id.tvCreateResume://新建简历
                createResume();
                break;
            case R.id.ivAllResume://所有简历
                if (listShow.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_create_resume));
                    return;
                }
                startActivity(AllResumeActivity.class);
                break;
            case R.id.ivGenerate://生成
                if (listShow.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_create_resume));
                    return;
                }
                generate();
                break;
        }
    }

    @Override
    protected void initData() {
        ResumeDataUtils.initModuleData();
//        test();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<ResumeBean> listSp = ResumeDataUtils.getResumeList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            setResumeData();
            mDataBinding.scResume.setVisibility(View.VISIBLE);
            mDataBinding.llResumeNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.scResume.setVisibility(View.GONE);
            mDataBinding.llResumeNoData.setVisibility(View.VISIBLE);
        }
    }

    @SuppressLint("NewApi")
    private void setResumeData() {
        ResumeBean currentBean = listShow.get(0);
        List<ResumeBean.Module> listModule = currentBean.getResumeData();
        mDataBinding.llContent.removeAllViews();
        for (int i = 0; i < listModule.size(); i++) {
            int finalI = i;
            ResumeBean.Module module = listModule.get(i);
            if (!module.isShow()) continue;
            View itemView = null;
            if (module.getName().equals(BASIC_INFO.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.BaseInfoData baseInfoData = GsonUtils.fromJson(GsonUtils.toJson(module.getData()), ResumeBean.BaseInfoData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_basic_info, mDataBinding.llContent, false);
                    ImageView ivPhoto = itemView.findViewById(R.id.ivPhoto);
                    ImageView ivSex = itemView.findViewById(R.id.ivSex);
                    TextView tvName = itemView.findViewById(R.id.tvName);
                    TextView tvSex = itemView.findViewById(R.id.tvSex);
                    TextView tvBirthday = itemView.findViewById(R.id.tvBirthday);
                    TextView tvWorkTime = itemView.findViewById(R.id.tvWorkTime);
                    TextView tvPhone = itemView.findViewById(R.id.tvPhone);
                    TextView tvEmail = itemView.findViewById(R.id.tvEmail);
                    if (baseInfoData.getAvatar() != null) {
                        Glide.with(mContext).load(baseInfoData.getAvatar()).into(ivPhoto);
                    } else {
                        ivPhoto.setImageResource(R.drawable.atjzp);
                    }
                    tvName.setText(baseInfoData.getName());
                    if (baseInfoData.getSex().equals("男")) {
                        ivSex.setImageResource(R.drawable.axbn);
                    } else {
                        ivSex.setImageResource(R.drawable.axbnv);
                    }
                    tvSex.setText(baseInfoData.getSex());
                    tvBirthday.setText(CalculatorUtil.calculateAge(baseInfoData.getDateOfBirth()) + getString(R.string.age_text));
                    tvPhone.setText(baseInfoData.getPhone());
                    tvEmail.setText(baseInfoData.getEmail());
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.JOB_INTENTION.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.JobData jobData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.JobData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_other, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    TextView tvContent = itemView.findViewById(R.id.tvContent);
                    tvTitle.setText(module.getAlias());
                    tvContent.setText(jobData.getTargetJob() + "   " + jobData.getTargetCity() + "   " + jobData.getEntryTime() + "   " + jobData.getTargetSalary());
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.EDUCATION_EXP.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.EducationInfoData educationInfoData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.EducationInfoData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_exp, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    ImageView ivAdd = itemView.findViewById(R.id.ivAdd);
                    RecyclerView recyclerView = itemView.findViewById(R.id.rvExpList);
                    tvTitle.setText(module.getAlias());
                    recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                    ExpAdapter expAdapter = new ExpAdapter();
                    recyclerView.setAdapter(expAdapter);
                    List<ExpBean> list = new ArrayList<>();
                    educationInfoData.setEducationExperienceList(GsonUtils.fromJson(educationInfoData.getEducationExperienceList().toString(), new TypeToken<List<ResumeBean.EducationExperience>>() {
                    }.getType()));
                    for (ResumeBean.EducationExperience educationExperience : educationInfoData.getEducationExperienceList()) {
                        educationExperience.setDetailList(GsonUtils.fromJson(educationExperience.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                        }.getType()));
                        String title = educationExperience.getSchoolName() + " | " + educationExperience.getQualification() + " | " +
                                educationExperience.getMajor() + " | " + educationExperience.getStartTime() + " ～ " +
                                educationExperience.getEndTime();
                        String detail = "";
                        for (ResumeBean.DetailItem detailItem : educationExperience.getDetailList()) {
                            detail += detailItem.getText() + "\n";
                        }
                        list.add(new ExpBean(title, detail));
                    }
                    expAdapter.setList(list);
                    expAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                            EducationExpActivity.position = selPos;
                            EducationExpActivity.modulePos = finalI;
                            EducationExpActivity.dataPos = position;
                            startActivity(EducationExpActivity.class);
                        }
                    });
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.WORK_EXP.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.WorkExperienceData workExperienceData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.WorkExperienceData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_exp, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    ImageView ivAdd = itemView.findViewById(R.id.ivAdd);
                    RecyclerView recyclerView = itemView.findViewById(R.id.rvExpList);
                    tvTitle.setText(module.getAlias());
                    recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                    ExpAdapter expAdapter = new ExpAdapter();
                    recyclerView.setAdapter(expAdapter);
                    List<ExpBean> list = new ArrayList<>();
                    workExperienceData.setWorkExperienceList(GsonUtils.fromJson(workExperienceData.getWorkExperienceList().toString(), new TypeToken<List<ResumeBean.WorkExperience>>() {
                    }.getType()));
                    for (ResumeBean.WorkExperience workExperience : workExperienceData.getWorkExperienceList()) {
                        String title = workExperience.getCompanyName() + " | " + workExperience.getJob() + " | " +
                                workExperience.getStartTime() + " ~ " + workExperience.getEndTime();
                        String detail = "";
                        workExperience.setDetailList(GsonUtils.fromJson(workExperience.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                        }.getType()));
                        for (ResumeBean.DetailItem detailItem : workExperience.getDetailList()) {
                            detail += detailItem.getText() + "\n";
                        }
                        list.add(new ExpBean(title, detail));
                    }
                    expAdapter.setList(list);
                    expAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                            WorkExpActivity.position = selPos;
                            WorkExpActivity.modulePos = finalI;
                            WorkExpActivity.dataPos = position;
                            startActivity(WorkExpActivity.class);
                        }
                    });
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.CAMPUS_ACTIVITY.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.SchoolExperienceData schoolExperienceData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.SchoolExperienceData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_exp, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    ImageView ivAdd = itemView.findViewById(R.id.ivAdd);
                    RecyclerView recyclerView = itemView.findViewById(R.id.rvExpList);
                    tvTitle.setText(module.getAlias());
                    recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                    ExpAdapter expAdapter = new ExpAdapter();
                    recyclerView.setAdapter(expAdapter);
                    List<ExpBean> list = new ArrayList<>();
                    schoolExperienceData.setSchoolExperienceList(GsonUtils.fromJson(schoolExperienceData.getSchoolExperienceList().toString(), new TypeToken<List<ResumeBean.SchoolExperience>>() {
                    }.getType()));
                    for (ResumeBean.SchoolExperience schoolExperience : schoolExperienceData.getSchoolExperienceList()) {
                        String title = schoolExperience.getProjectName() + " | " + schoolExperience.getRoleName() + " | " +
                                schoolExperience.getStartTime() + " ~ " + schoolExperience.getEndTime();
                        String detail = "";
                        schoolExperience.setDetailList(GsonUtils.fromJson(schoolExperience.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                        }.getType()));
                        for (ResumeBean.DetailItem detailItem : schoolExperience.getDetailList()) {
                            detail += detailItem.getText() + "\n";
                        }
                        list.add(new ExpBean(title, detail));
                    }
                    expAdapter.setList(list);
                    expAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                            CampusActActivity.position = selPos;
                            CampusActActivity.modulePos = finalI;
                            CampusActActivity.dataPos = position;
                            startActivity(CampusActActivity.class);
                        }
                    });
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.INTERNSHIP_EXP.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.InternShipExperienceData internShipExperienceData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.InternShipExperienceData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_exp, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    ImageView ivAdd = itemView.findViewById(R.id.ivAdd);
                    RecyclerView recyclerView = itemView.findViewById(R.id.rvExpList);
                    tvTitle.setText(module.getAlias());
                    recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                    ExpAdapter expAdapter = new ExpAdapter();
                    recyclerView.setAdapter(expAdapter);
                    List<ExpBean> list = new ArrayList<>();
                    internShipExperienceData.setInternShipExperienceList(GsonUtils.fromJson(internShipExperienceData.getInternShipExperienceList().toString(), new TypeToken<List<ResumeBean.InternShipExperience>>() {
                    }.getType()));
                    for (ResumeBean.InternShipExperience internShipExperience : internShipExperienceData.getInternShipExperienceList()) {
                        String title = internShipExperience.getCompanyName() + " | " + internShipExperience.getJob() + " | " +
                                internShipExperience.getStartTime() + " ~ " + internShipExperience.getEndTime();
                        String detail = "";
                        internShipExperience.setDetailList(GsonUtils.fromJson(internShipExperience.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                        }.getType()));
                        for (ResumeBean.DetailItem detailItem : internShipExperience.getDetailList()) {
                            detail += detailItem.getText() + "\n";
                        }
                        list.add(new ExpBean(title, detail));
                    }
                    expAdapter.setList(list);
                    expAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                            InternshipExpActivity.position = selPos;
                            InternshipExpActivity.modulePos = finalI;
                            InternshipExpActivity.dataPos = position;
                            startActivity(InternshipExpActivity.class);
                        }
                    });
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.PROJECT_EXP.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.ProjectExperienceData projectExperienceData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.ProjectExperienceData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_exp, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    ImageView ivAdd = itemView.findViewById(R.id.ivAdd);
                    RecyclerView recyclerView = itemView.findViewById(R.id.rvExpList);
                    tvTitle.setText(module.getAlias());
                    recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                    ExpAdapter expAdapter = new ExpAdapter();
                    recyclerView.setAdapter(expAdapter);
                    List<ExpBean> list = new ArrayList<>();
                    projectExperienceData.setProjectExperienceList(GsonUtils.fromJson(projectExperienceData.getProjectExperienceList().toString(), new TypeToken<List<ResumeBean.ProjectExperience>>() {
                    }.getType()));
                    for (ResumeBean.ProjectExperience projectExperience : projectExperienceData.getProjectExperienceList()) {
                        String title = projectExperience.getProjectName() + " | " + projectExperience.getRoleName() + " | " +
                                projectExperience.getStartTime() + " ~ " + projectExperience.getEndTime();
                        String detail = "";
                        projectExperience.setDetailList(GsonUtils.fromJson(projectExperience.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                        }.getType()));
                        for (ResumeBean.DetailItem detailItem : projectExperience.getDetailList()) {
                            detail += detailItem.getText() + "\n";
                        }
                        list.add(new ExpBean(title, detail));
                    }
                    expAdapter.setList(list);
                    expAdapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                            ProjectExpActivity.position = selPos;
                            ProjectExpActivity.modulePos = finalI;
                            ProjectExpActivity.dataPos = position;
                            startActivity(ProjectExpActivity.class);
                        }
                    });
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.PROFESSIONAL_SKILL.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.SkillData skillData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.SkillData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_skill, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    RecyclerView recyclerView = itemView.findViewById(R.id.rvSkillList);
                    tvTitle.setText(module.getAlias());
                    FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(mContext);
                    layoutManager.setFlexDirection(FlexDirection.ROW);
                    layoutManager.setJustifyContent(JustifyContent.FLEX_START);
                    recyclerView.setLayoutManager(layoutManager);
                    SkillAdapter skillAdapter = new SkillAdapter();
                    skillAdapter.setEdit(false);
                    recyclerView.setAdapter(skillAdapter);
                    skillData.setDetailList(GsonUtils.fromJson(skillData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    skillAdapter.setList(skillData.getDetailList());
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.HONORARY_CERTIFICATE.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.CertificateData certificateData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.CertificateData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_other, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    TextView tvContent = itemView.findViewById(R.id.tvContent);
                    tvTitle.setText(module.getAlias());
                    String detail = "";
                    certificateData.setDetailList(GsonUtils.fromJson(certificateData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    for (ResumeBean.DetailItem detailItem : certificateData.getDetailList()) {
                        detail += detailItem.getText() + "\n";
                    }
                    certificateData.setCertificateList(GsonUtils.fromJson(certificateData.getCertificateList().toString(), new TypeToken<List<ResumeBean.CertificateItem>>() {
                    }.getType()));
                    for (ResumeBean.CertificateItem certificateItem : certificateData.getCertificateList()) {
                        detail += certificateItem.getText() + "\n";
                    }
                    tvContent.setText(detail);
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.HOBBIES_INTERESTS.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.HobbyData hobbyData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.HobbyData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_other, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    TextView tvContent = itemView.findViewById(R.id.tvContent);
                    tvTitle.setText(module.getAlias());
                    String detail = "";
                    hobbyData.setDetailList(GsonUtils.fromJson(hobbyData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    for (ResumeBean.DetailItem detailItem : hobbyData.getDetailList()) {
                        detail += detailItem.getText() + "\n";
                    }
                    tvContent.setText(detail);
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.SELF_EVALUATION.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.SelfEvaluationData selfEvaluationData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.SelfEvaluationData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_other, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    TextView tvContent = itemView.findViewById(R.id.tvContent);
                    tvTitle.setText(module.getAlias());
                    String detail = "";
                    selfEvaluationData.setDetailList(GsonUtils.fromJson(selfEvaluationData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    for (ResumeBean.DetailItem detailItem : selfEvaluationData.getDetailList()) {
                        detail += detailItem.getText() + "\n";
                    }
                    tvContent.setText(detail);
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            } else if (module.getName().equals(ModuleCategory.CUSTOM_TITLE.getCode())) {
                if (module.getData() != null) {
                    ResumeBean.CustomModuleData customModuleData = GsonUtils.fromJson(module.getData().toString(), ResumeBean.CustomModuleData.class);
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.layout_other, mDataBinding.llContent, false);
                    TextView tvTitle = itemView.findViewById(R.id.tvTitle);
                    TextView tvContent = itemView.findViewById(R.id.tvContent);
                    tvTitle.setText(module.getAlias());
                    String detail = "";
                    customModuleData.setDetailList(GsonUtils.fromJson(customModuleData.getDetailList().toString(), new TypeToken<List<ResumeBean.DetailItem>>() {
                    }.getType()));
                    for (ResumeBean.DetailItem detailItem : customModuleData.getDetailList()) {
                        detail += detailItem.getText() + "\n";
                    }
                    tvContent.setText(detail);
                } else {
                    itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_module, mDataBinding.llContent, false);
                    TextView tvName = itemView.findViewById(R.id.tvModuleItemName);
                    tvName.setText(module.getAlias());
                }
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (ModuleCategory.fromCode(module.getName())) {
                        case BASIC_INFO://基本信息
                            BasicInfoActivity.position = selPos;
                            BasicInfoActivity.modulePos = finalI;
                            startActivity(BasicInfoActivity.class);
                            break;
                        case JOB_INTENTION://求职意向
                            JobIntentionActivity.position = selPos;
                            JobIntentionActivity.modulePos = finalI;
                            startActivity(JobIntentionActivity.class);
                            break;
                        case EDUCATION_EXP://教育经历
                            EducationExpActivity.position = selPos;
                            EducationExpActivity.modulePos = finalI;
                            EducationExpActivity.dataPos = -1;
                            startActivity(EducationExpActivity.class);
                            break;
                        case WORK_EXP://工作经历
                            WorkExpActivity.position = selPos;
                            WorkExpActivity.modulePos = finalI;
                            WorkExpActivity.dataPos = -1;
                            startActivity(WorkExpActivity.class);
                            break;
                        case PROFESSIONAL_SKILL://专业技能
                            ProfessionalSkillActivity.position = selPos;
                            ProfessionalSkillActivity.modulePos = finalI;
                            startActivity(ProfessionalSkillActivity.class);
                            break;
                        case CAMPUS_ACTIVITY://校园活动
                            CampusActActivity.position = selPos;
                            CampusActActivity.modulePos = finalI;
                            CampusActActivity.dataPos = -1;
                            startActivity(CampusActActivity.class);
                            break;
                        case INTERNSHIP_EXP://实习经历
                            InternshipExpActivity.position = selPos;
                            InternshipExpActivity.modulePos = finalI;
                            InternshipExpActivity.dataPos = -1;
                            startActivity(InternshipExpActivity.class);
                            break;
                        case PROJECT_EXP://项目经验
                            ProjectExpActivity.position = selPos;
                            ProjectExpActivity.modulePos = finalI;
                            ProjectExpActivity.dataPos = -1;
                            startActivity(ProjectExpActivity.class);
                            break;
                        case HONORARY_CERTIFICATE://荣誉证书
                            CertificateActivity.position = selPos;
                            CertificateActivity.modulePos = finalI;
                            startActivity(CertificateActivity.class);
                            break;
                        default:
                            OtherInfoActivity.moduleCategory = module.getName();
                            OtherInfoActivity.position = selPos;
                            OtherInfoActivity.modulePos = finalI;
                            startActivity(OtherInfoActivity.class);
                            break;
                    }
                }
            });
            mDataBinding.llContent.addView(itemView);
        }
    }

    private void createResume() {
        List<ResumeBean> listSave = new ArrayList<>();
        ResumeBean resumeBean = new ResumeBean();
        resumeBean.setResumeData(ResumeDataUtils.getModuleList());
        listSave.add(resumeBean);
        //保存数据
        List<ResumeBean> listSp = ResumeDataUtils.getResumeList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            ResumeDataUtils.saveResumeList(listSp);
        } else {
            ResumeDataUtils.saveResumeList(listSave);
        }
        ToastUtils.showShort(getContext().getString(R.string.create_resume_suc));
        getData();
    }

    private void share() {
        showDialog(getString(R.string.loading));
        String filePath = FileUtil.generateFilePath(Content.myTemp, ".png");
        String pdfPath = FileUtil.generateFilePath(Content.myTemp, ".pdf");
        ImageUtils.save(ViewToBitmapUtils.convertWideView(mDataBinding.scResume), filePath, Bitmap.CompressFormat.PNG);
        List<String> list = new ArrayList<>();
        list.add(filePath);
        try {
            ImageToPdfUtils.convertImageToPdf(mContext, filePath, new File(pdfPath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                IntentUtil.shareFile(mContext, pdfPath);
            }
        }, 1000);
    }

    private void test() {
        ResumeBean ResumeBean = new ResumeBean();
        List<ResumeBean.Module> list = new ArrayList<>();

        ResumeBean.Module baseInfoModule = new ResumeBean.Module();
        baseInfoModule.setName("baseInfoModule");
        baseInfoModule.setAlias("");
//        baseInfoModule.setAlias("基本信息");
        baseInfoModule.setShow(true);
        ResumeBean.BaseInfoData baseInfoData = new ResumeBean.BaseInfoData();
        baseInfoData.setName("司空震");
        baseInfoData.setSex("男");
        baseInfoData.setDateOfBirth("1996-01");
        baseInfoData.setBorth2Age(false);
        baseInfoData.setAvatar("");
        baseInfoData.setShowAvatar(false);
        baseInfoData.setPhone("18812345678");
        baseInfoData.setEmail("wangzhe@qq.com");
        baseInfoData.setMaritalStatus("未婚");
        baseInfoData.setHeight("180cm");
        baseInfoData.setWeight("70kg");
        baseInfoData.setNation("汉");
        baseInfoData.setNativePlace("湖南省长沙市");
        baseInfoData.setPoliticalStatus("群众");
        baseInfoModule.setData(baseInfoData);
        list.add(baseInfoModule);

        ResumeBean.Module jobModule = new ResumeBean.Module();
        jobModule.setName("jobModule");
//        jobModule.setAlias("求职意向");
        jobModule.setAlias("");
        jobModule.setShow(true);
        ResumeBean.JobData jobData = new ResumeBean.JobData();
        jobData.setTargetJob("前端工程师");
        jobData.setTargetCity("长沙");
        jobData.setTargetSalary("面议");
        jobData.setEntryTime("随时待岗");
        jobModule.setData(jobData);
        list.add(jobModule);

        ResumeBean.Module applicationInfoModule = new ResumeBean.Module();
        applicationInfoModule.setName("applicationInfoModule");
        applicationInfoModule.setAlias("报考信息");
        applicationInfoModule.setShow(false);
        ResumeBean.ApplicationInfoData applicationInfoData = new ResumeBean.ApplicationInfoData();
        applicationInfoData.setApplySchool("长沙国防科技大学");
        applicationInfoData.setApplyMajor("软件工程");
        applicationInfoData.setScoreName("初试成绩");
        List<ResumeBean.ScoreItem> scoreItemList = new ArrayList<>();
        scoreItemList.add(new ResumeBean.ScoreItem("数学", 122));
        scoreItemList.add(new ResumeBean.ScoreItem("英语", 120));
        scoreItemList.add(new ResumeBean.ScoreItem("计算机综合", 120));
        scoreItemList.add(new ResumeBean.ScoreItem("政治", 98));
        applicationInfoData.setScoreList(new ArrayList<>());
        applicationInfoModule.setData(applicationInfoData);
        list.add(applicationInfoModule);

        ResumeBean.Module educationInfoModule = new ResumeBean.Module();
        educationInfoModule.setName("educationInfoModule");
        educationInfoModule.setAlias("教育背景");
        educationInfoModule.setShow(true);
        ResumeBean.EducationInfoData educationInfoData = new ResumeBean.EducationInfoData();
        List<ResumeBean.EducationExperience> educationExperienceList = new ArrayList<>();
        ResumeBean.EducationExperience educationExperience = new ResumeBean.EducationExperience();
        educationExperience.setSchoolName("湖南大学");
        educationExperience.setMajor("软件工程");
        educationExperience.setStartTime("2016-09");
        educationExperience.setEndTime("2020-07");
        educationExperience.setQualification("本科");
        List<ResumeBean.DetailItem> detailItemList = new ArrayList<>();
        detailItemList.add(new ResumeBean.DetailItem("获得了全国计算机等级考试二级证书"));
        detailItemList.add(new ResumeBean.DetailItem("参与了多个项目的开发和维护"));
        educationExperience.setDetailList(detailItemList);
        educationExperienceList.add(educationExperience);
        educationInfoData.setEducationExperienceList(educationExperienceList);
        educationInfoModule.setData(educationInfoData);
        list.add(educationInfoModule);


        ResumeBean.Module projectExperienceModule = new ResumeBean.Module();
        projectExperienceModule.setName("projectExperienceModule");
        projectExperienceModule.setAlias("项目经历");
        projectExperienceModule.setShow(true);
        ResumeBean.ProjectExperienceData projectExperienceData = new ResumeBean.ProjectExperienceData();
        List<ResumeBean.ProjectExperience> projectExperienceList = new ArrayList<>();
        ResumeBean.ProjectExperience projectExperience = new ResumeBean.ProjectExperience();
        projectExperience.setProjectName("公司内部数据中台");
        projectExperience.setRoleName("开发者");
        projectExperience.setStartTime("2020-07");
        projectExperience.setEndTime("2022-07");
        List<ResumeBean.DetailItem> projectDetailList = new ArrayList<>();
        projectDetailList.add(new ResumeBean.DetailItem("该项目是用于公司内部的内部数据中台"));
        projectDetailList.add(new ResumeBean.DetailItem("该项目解决了公司内部的数据管理问题"));
        projectExperience.setDetailList(projectDetailList);
        projectExperienceList.add(projectExperience);
        projectExperienceData.setProjectExperienceList(projectExperienceList);
        projectExperienceModule.setData(projectExperienceData);
        list.add(projectExperienceModule);

        // 添加工作经历模块
        ResumeBean.Module workExperienceModule = new ResumeBean.Module();
        workExperienceModule.setName("workExperienceModule");
        workExperienceModule.setAlias("工作经历");
        workExperienceModule.setShow(true);
        ResumeBean.WorkExperienceData workExperienceData = new ResumeBean.WorkExperienceData();
        List<ResumeBean.WorkExperience> workExperienceList = new ArrayList<>();
        ResumeBean.WorkExperience workExperience = new ResumeBean.WorkExperience();
        workExperience.setCompanyName("赤焰网络公司");
        workExperience.setJob("全栈工程师");
        workExperience.setStartTime("2020-07");
        workExperience.setEndTime("2022-07");
        List<ResumeBean.DetailItem> detailList = new ArrayList<>();
        detailList.add(new ResumeBean.DetailItem("负责公司的前端项目的开发和维护"));
        detailList.add(new ResumeBean.DetailItem("参与了多个项目的开发和维护"));
        workExperience.setDetailList(detailList);
        workExperienceList.add(workExperience);
        workExperienceData.setWorkExperienceList(workExperienceList);
        workExperienceModule.setData(workExperienceData);
        list.add(workExperienceModule);

        // 添加实习经历模块
        ResumeBean.Module internShipExperienceModule = new ResumeBean.Module();
        internShipExperienceModule.setName("internShipExperienceModule");
        internShipExperienceModule.setAlias("实习经历");
        internShipExperienceModule.setShow(true);
        ResumeBean.InternShipExperienceData internShipExperienceData = new ResumeBean.InternShipExperienceData();
        List<ResumeBean.InternShipExperience> internShipExperienceList = new ArrayList<>();
        ResumeBean.InternShipExperience internShipExperience = new ResumeBean.InternShipExperience();
        internShipExperience.setCompanyName("赤焰网络公司");
        internShipExperience.setJob("前端工程师");
        internShipExperience.setStartTime("2020-07");
        internShipExperience.setEndTime("2020-10");
        List<ResumeBean.DetailItem> internDetailList = new ArrayList<>();
        internDetailList.add(new ResumeBean.DetailItem("负责了xx项目的开发和维护"));
        internDetailList.add(new ResumeBean.DetailItem("该项目解决了公司内部的数据管理问题"));
        internShipExperience.setDetailList(internDetailList);
        internShipExperienceList.add(internShipExperience);
        internShipExperienceData.setInternShipExperienceList(internShipExperienceList);
        internShipExperienceModule.setData(internShipExperienceData);
        list.add(internShipExperienceModule);

        // 添加校园经历模块
        ResumeBean.Module schoolExperienceModule = new ResumeBean.Module();
        schoolExperienceModule.setName("schoolExperienceModule");
        schoolExperienceModule.setAlias("校园经历");
        schoolExperienceModule.setShow(true);
        ResumeBean.SchoolExperienceData schoolExperienceData = new ResumeBean.SchoolExperienceData();
        List<ResumeBean.SchoolExperience> schoolExperienceList = new ArrayList<>();
        ResumeBean.SchoolExperience schoolExperience = new ResumeBean.SchoolExperience();
        schoolExperience.setProjectName("大学生创新创业大赛");
        schoolExperience.setRoleName("发起人");
        schoolExperience.setStartTime("2016-09");
        schoolExperience.setEndTime("2020-07");
        List<ResumeBean.DetailItem> schoolDetailList = new ArrayList<>();
        schoolDetailList.add(new ResumeBean.DetailItem("获得了全国计算机等级考试二级证书"));
        schoolDetailList.add(new ResumeBean.DetailItem("参与了多个项目的开发和维护"));
        schoolExperience.setDetailList(schoolDetailList);
        schoolExperienceList.add(schoolExperience);
        schoolExperienceData.setSchoolExperienceList(schoolExperienceList);
        schoolExperienceModule.setData(schoolExperienceData);
        list.add(schoolExperienceModule);

        // 添加技能特长模块
        ResumeBean.Module skillModule = new ResumeBean.Module();
        skillModule.setName("skillModule");
        skillModule.setAlias("技能特长");
        skillModule.setShow(true);
        ResumeBean.SkillData skillData = new ResumeBean.SkillData();
        List<ResumeBean.DetailItem> skillDetailList = new ArrayList<>();
        skillDetailList.add(new ResumeBean.DetailItem("熟悉React、Taro等前端框架"));
        skillDetailList.add(new ResumeBean.DetailItem("熟悉Koa等后端框架"));
        skillDetailList.add(new ResumeBean.DetailItem("熟悉Node.js、数据库等"));
        skillData.setDetailList(skillDetailList);
        List<ResumeBean.SkillItem> skillList = new ArrayList<>();
        ResumeBean.SkillItem skillItem1 = new ResumeBean.SkillItem();
        skillItem1.setName("Javascript");
        skillItem1.setType("percentage");
        skillItem1.setLevelPercentage(80);
        skillItem1.setLevelText("熟悉");
        skillList.add(skillItem1);
        ResumeBean.SkillItem skillItem2 = new ResumeBean.SkillItem();
        skillItem2.setName("React");
        skillItem2.setType("text");
        skillItem2.setLevelPercentage(90);
        skillItem2.setLevelText("熟练");
        skillList.add(skillItem2);
        ResumeBean.SkillItem skillItem3 = new ResumeBean.SkillItem();
        skillItem3.setName("NodeJS");
        skillItem3.setType("percentage");
        skillItem3.setLevelPercentage(80);
        skillItem3.setLevelText("熟悉");
        skillList.add(skillItem3);
        ResumeBean.SkillItem skillItem4 = new ResumeBean.SkillItem();
        skillItem4.setName("数据库");
        skillItem4.setType("text");
        skillItem4.setLevelPercentage(90);
        skillItem4.setLevelText("熟练");
        skillList.add(skillItem4);
        skillData.setSkillList(skillList);
        skillModule.setData(skillData);
        list.add(skillModule);

        // 添加荣誉证书模块
        ResumeBean.Module certificateModule = new ResumeBean.Module();
        certificateModule.setName("certificateModule");
        certificateModule.setAlias("荣誉证书");
        certificateModule.setShow(true);
        ResumeBean.CertificateData certificateData = new ResumeBean.CertificateData();
        List<ResumeBean.DetailItem> certificateDetailList = new ArrayList<>();
        certificateDetailList.add(new ResumeBean.DetailItem("获得了全国计算机等级考试二级证书"));
        certificateDetailList.add(new ResumeBean.DetailItem("在xx活动中获得了三好学生证书"));
        certificateData.setDetailList(certificateDetailList);
        List<ResumeBean.CertificateItem> certificateList = new ArrayList<>();
        ResumeBean.CertificateItem certItem1 = new ResumeBean.CertificateItem();
        certItem1.setText("十大杰出青年");
        certificateList.add(certItem1);
        ResumeBean.CertificateItem certItem2 = new ResumeBean.CertificateItem();
        certItem2.setText("优秀共青团员");
        certificateList.add(certItem2);
        ResumeBean.CertificateItem certItem3 = new ResumeBean.CertificateItem();
        certItem3.setText("优秀学生干部");
        certificateList.add(certItem3);
        certificateData.setCertificateList(certificateList);
        certificateModule.setData(certificateData);
        list.add(certificateModule);

        // 添加自我评价模块
        ResumeBean.Module selfEvaluationModule = new ResumeBean.Module();
        selfEvaluationModule.setName("selfEvaluationModule");
        selfEvaluationModule.setAlias("自我评价");
        selfEvaluationModule.setShow(true);
        ResumeBean.SelfEvaluationData selfEvaluationData = new ResumeBean.SelfEvaluationData();
        List<ResumeBean.DetailItem> selfEvalDetailList = new ArrayList<>();
        selfEvalDetailList.add(new ResumeBean.DetailItem("我是一个优秀的学生"));
        selfEvalDetailList.add(new ResumeBean.DetailItem("我是一个安静的美男子"));
        selfEvalDetailList.add(new ResumeBean.DetailItem("我是一个乐观的人"));
        selfEvaluationData.setDetailList(selfEvalDetailList);
        selfEvaluationModule.setData(selfEvaluationData);
        list.add(selfEvaluationModule);

        // 添加兴趣爱好模块
        ResumeBean.Module hobbyModule = new ResumeBean.Module();
        hobbyModule.setName("hobbyModule");
        hobbyModule.setAlias("兴趣爱好");
        hobbyModule.setShow(true);
        ResumeBean.HobbyData hobbyData = new ResumeBean.HobbyData();
        List<ResumeBean.DetailItem> hobbyDetailList = new ArrayList<>();
        hobbyDetailList.add(new ResumeBean.DetailItem("我经常打篮球，唱跳rap"));
        hobbyDetailList.add(new ResumeBean.DetailItem("我经常穿背带裤"));
        hobbyDetailList.add(new ResumeBean.DetailItem("我经常梳中分，自认坤门第一"));
        hobbyData.setDetailList(hobbyDetailList);
        List<ResumeBean.HobbyItem> hobbyList = new ArrayList<>();
        ResumeBean.HobbyItem hobbyItem1 = new ResumeBean.HobbyItem();
        hobbyItem1.setText("篮球");
        hobbyList.add(hobbyItem1);
        ResumeBean.HobbyItem hobbyItem2 = new ResumeBean.HobbyItem();
        hobbyItem2.setText("足球");
        hobbyList.add(hobbyItem2);
        ResumeBean.HobbyItem hobbyItem3 = new ResumeBean.HobbyItem();
        hobbyItem3.setText("羽毛球");
        hobbyList.add(hobbyItem3);
        hobbyData.setHobbyList(hobbyList);
        hobbyModule.setData(hobbyData);
        list.add(hobbyModule);

        // 添加自定义模块
        ResumeBean.Module customModule = new ResumeBean.Module();
        customModule.setName("customModule");
        customModule.setAlias("自定义模块");
        customModule.setShow(true);
        ResumeBean.CustomModuleData customData = new ResumeBean.CustomModuleData();
        List<ResumeBean.DetailItem> customDetailList = new ArrayList<>();
        customDetailList.add(new ResumeBean.DetailItem("我是一个优秀的自定义"));
        customDetailList.add(new ResumeBean.DetailItem("我是一个安静的美男子自定义"));
        customDetailList.add(new ResumeBean.DetailItem("我是一个乐观的自定义"));
        customData.setDetailList(customDetailList);
        customModule.setData(customData);
        list.add(customModule);

        ResumeBean.setResumeData(list);

        OkHttpPostUtil.postJson(AppConfig.RESUME_GENERATE_DATA, new Gson().toJson(ResumeBean), new OkHttpPostUtil.PostCallback() {
            @Override
            public void onSuccess(String response) {
                PreviewActivity.response = response;
                startActivity(PreviewActivity.class);
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    private void generate() {
        for (ResumeBean.Module resumeDatum : listShow.get(0).getResumeData()) {
            if (resumeDatum.isShow()) {
                if (resumeDatum.getData() == null) {
                    ToastUtils.showShort(getString(R.string.please_input_resume_data));
                    return;
                }
            }
        }
        OkHttpPostUtil.postJson(AppConfig.RESUME_GENERATE_DATA, new Gson().toJson(listShow.get(0)), new OkHttpPostUtil.PostCallback() {
            @Override
            public void onSuccess(String response) {
                PreviewActivity.response = response;
                startActivity(PreviewActivity.class);
            }

            @Override
            public void onFailure(Exception e) {
                ToastUtils.showShort(e.getMessage());
            }
        });
    }
}